/*
 * AbstractShowWindowAction.java
 * 
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.actions.system;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import org.tiberiumlabs.lailaps.actions.AbstractResourcedAction;
import org.tiberiumlabs.lailaps.gui.GUIBuilder;
import org.tiberiumlabs.lailaps.tray.TrayFactory;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public abstract class AbstractShowWindowAction extends AbstractResourcedAction implements WindowListener {
    
    protected abstract boolean isWindowVisible();
    protected abstract boolean isSecondWindowVisible();
    protected abstract Window getWindow();
    protected abstract String getHideName();
    protected abstract String getHideShortDescription();
    protected abstract String getHideLongDescription();
    protected abstract String getShowName();
    protected abstract String getShowShortDescription();
    protected abstract String getShowLongDescription();
    protected abstract ImageIcon getIcon();

    protected GUIBuilder guiBuilder = mediator.getGuiBuilder();
    
    public AbstractShowWindowAction() {
        putValue(SMALL_ICON, getIcon());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (TrayFactory.isTrayVisible()) {
            if (isWindowVisible()) {
                getWindow().dispose();
            }
            else {
                getWindow().setVisible(true);
            }
        }
        else {
            if (isSecondWindowVisible()) {
                getWindow().dispose();
            }
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {
        revalidateName();
    }

    @Override
    public void windowClosed(WindowEvent e) {
        revalidateName();
    }

    protected void revalidateName() {
        String name;
        String shortDesc;
        String longDesc;
        if (isWindowVisible()) {
            name = getHideName();
            shortDesc = getHideShortDescription();
            longDesc = getHideLongDescription();
            putValue(SELECTED_KEY, Boolean.TRUE);
        }
        else {
            name = getShowName();
            shortDesc = getShowShortDescription();
            longDesc = getShowLongDescription();
            putValue(SELECTED_KEY, Boolean.FALSE);
        }
        putValue(NAME, name);
        putValue(SHORT_DESCRIPTION, shortDesc);
        putValue(LONG_DESCRIPTION, longDesc);
    }
    
    // <editor-fold defaultstate="collapsed" desc=" implementing window listener ">
    @Override
    public void windowClosing(WindowEvent e) {
    }
    @Override
    public void windowIconified(WindowEvent e) {
    }
    @Override
    public void windowDeiconified(WindowEvent e) {
    }
    @Override
    public void windowActivated(WindowEvent e) {
    }
    @Override
    public void windowDeactivated(WindowEvent e) {
    }
    // </editor-fold>

}
